/**
 * 基础底层接口
 */
const _ = require('lodash');

/**
 * 所有定义的props
 */
export interface BProps {
  title?: string; //顶部名称
  componentId: string;
  state?: any;
  navigator?: any;
  actions?: any;
}
/**
 * 所有定义的state
 */

export interface BState {}

/**
 * 定义发送通知的接口，以及注册移除的监听
 */
export interface EnoadListener {
  addListener(listener: ExListener): any;
  delListener(listener: ExListener): any;
  sendMsg(tag: string, msg: string): any;
}

/**
 * 定义监听基础功能
 */
export interface ExListener {
  getTag(): string;
  init(param: string): any;
}

/**
 * 定义基础操作类，实现发送通知的接口信息。
 */
export class AbstractEnoad implements EnoadListener {
  enoad = new Array();

  addListener = (listener: ExListener) => {
    console.log('[注册]');
    let has = _.some(this.enoad, (item: ExListener) => {
      return item.getTag() == listener.getTag();
    });
    if (!has) {
      this.enoad.push(listener);
    }
    console.log('[注册]', this.enoad.length);
  };
  delListener = (listener: ExListener) => {
    console.log('[移除]');
    _.remove(this.enoad, (item: ExListener) => {
      return item.getTag() == listener.getTag();
    });
    console.log('[移除]', this.enoad.length);
  };

  sendMsg = (tag: string, msg: string) => {
    _.forEach(this.enoad, (listener: ExListener) => {
      if (listener.getTag() == tag) {
        console.log('[发送]' + msg);
        listener.init(msg);
      }
    });
  };
}
